



var ue = UE.getEditor('editor',{
});

$(function () {
/*    $("#jqGrid").jqGrid({
        url: baseURL + 'business/store/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '产品名称', name: 'productName', index: 'product_name', width: 80 },
			{ label: '类型', name: 'type', index: 'type', width: 80 },
			{ label: '展示方式', name: 'showStatus', index: 'show_status', width: 80,formatter:function (cellValue) {
				return harCodeName(cellValue,"","",showStatusJson,"");
			} },
			{ label: '状态', name: 'status', index: 'status', width: 80,formatter:function (cellValue) {
				return harCodeName(cellValue,"","",switchJson,"");
			} },
			{ label: '最近操作人', name: 'operator', index: 'operator', width: 80 },
			{ label: '操作时间', name: 'updateTime', index: 'update_time', width: 80 },
			{ label:'操作',formatter:function (cellValue,options,row) {
				var  template = "";

				template += '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.editor(null,\''+row.id+'\',\'1\')">查看</a>';

				if(row.status == 0){

					template += '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(null,\''+row.id+'\',\'1\')">启动</a>';

					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.editor(null,\''+row.id+'\',\'0\')">编辑</a>';

					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.del(null,\''+row.id+'\')">删除</a>';

				}else{
					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(null,\''+row.id+'\',\'0\')">禁用</a>';
				}

				return template;
			}}


        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
		loadComplete:function (r) {
			if(null != r.baseLoan){
				vm.baseLoan = r.baseLoan;
				$("#baseLoanType").tagsinput('add',vm.baseLoan.type);
				if(vm.baseLoan.image != null){
					vm.baseShow = true;
					vm.baseUrl = vm.baseLoan.image;
				}
			}
			if(null != r.types){
				vm.types = r.types;
			}
			
		}
    });*/

	//基础文件上传
	layui.use('upload', function(){
		layui.upload({
			url:'/admin/upload',
			success:function (r) {
				if(r.code  == '0'){
					vm.baseShow = true;
					vm.baseLoan.image = r.url;
					vm.baseUrl = r.url;
				}
			}
		});
	});

	//贷款文件上传
	layui.use('upload', function(){
		layui.upload({
			
			elem:".loanImage",
			url:'/admin/upload',
			success:function (r) {
				if(r.code  == '0'){
					vm.tFinanceProduct.image = r.url;
					vm.loanImageShow = true;
					vm.loanUrl = r.url;
				}
			}
		});
	});

	//input[radio] 切换特效
	$("input[type='radio']").change(function(){
			if($(this).val() == 0){
				vm.tFinanceProduct.status = 0;
			}else{
				vm.tFinanceProduct.status = 1;
			}
	});





});




var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tFinanceProduct: {
			showStatus:0,
			type:"",
			productDetails:'',
			image:""
		},
		baseMsg:'',
		baseLoan:{
			slogan:'',
			type:"",
			image:"",
			id:''
		},
		q:{
			productName:'',
			status:'',
			type:''
		},
		types:[],
		localShow:true,
		thirdShow:false,
		loanImageShow:false,
		loanUrl:"",
		isNoLook:true,
		baseShow:false,
		baseUrl:''
	},
	mounted:function () {
		this.$nextTick(function () {
			this.editor();
		})

	},
	methods: {
		baseSaveOrUpdate:function () {
			vm.baseLoan.type = $("#baseLoanType").val()
			var url = vm.baseLoan.id == '' ? "business/store/baseSave" : "business/store/baseUpdate";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.baseLoan),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tFinanceProduct = {
				showStatus:0
			};
		},
		update:function (event,id,status) {
			vm.getInfo(id);
			ue.setContent(vm.tFinanceProduct.productDetails+"")
			vm.tFinanceProduct.status = status;
			vm.saveOrUpdate();
		},
		editor: function () {
			ajax(baseURL + "business/store/info/",{},"get",false,false).then(function (r) {
				vm.tFinanceProduct = r.tFinanceProduct;
				vm.baseLoan = r.baseLoan;

			})
			//radio选择

			if(vm.tFinanceProduct != null) {

				if (vm.tFinanceProduct.status != null) {
					$("input[type='radio']").each(function () {
						if ($(this).val() == vm.tFinanceProduct.status) {
							console.log($(this).val());
							console.log(vm.tFinanceProduct.status);
							$(this).attr("checked", true);
						}
					});
				}
				if (vm.tFinanceProduct.productDetails != null){
					ue.ready(function () {
						ue.setContent(vm.tFinanceProduct.productDetails);
					})

				}

				if (vm.tFinanceProduct.image != '' && vm.tFinanceProduct.image != null) {
					vm.loanImageShow = true
					vm.loanUrl = vm.tFinanceProduct.image;
				}
			}else{
				vm.tFinanceProduct = {
					showStatus:0,
					type:"",
					productDetails:'',
					image:""
				}
			}

			//基础类型
			if(vm.baseLoan != null){
				if( vm.baseLoan.image != null){
					vm.baseShow = true;
					vm.baseUrl = vm.baseLoan.image;
				}
			}else{
				vm.baseLoan = {
					slogan:'',
					type:"",
					image:"",
					id:''
				}
			}
		},

		saveOrUpdate: function (event) {

			if(vm.baseLoan.id == null){
				alert("请先完成基础配置");
				return;
			}


			if(null == vm.tFinanceProduct.productName || vm.tFinanceProduct.productName.length== 0 ){
				alert("请输入产品名称");
				return;
			}

			if(null == vm.tFinanceProduct.image || vm.tFinanceProduct.image.length== 0 ){
				alert("请先上传图片");
				return;
			}
			if(null == vm.tFinanceProduct.productDesc || vm.tFinanceProduct.productDesc.length== 0 ){
				alert("请添加产品简述");
				return;
			}
			if(null == vm.tFinanceProduct.productDetails || vm.tFinanceProduct.productDetails.length== 0 ){
				alert("请添加产品详情");
				return;
			}

			var url = vm.tFinanceProduct.id == null ? "business/store/save" : "business/store/update";

			//展示方式

			vm.tFinanceProduct.productDetails =  ue.getContent();
			

			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.tFinanceProduct),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event,rowId) {

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "business/store/delete/"+rowId,
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(){
			ajax(baseURL + "business/store/info/",{},"get",false,false).then(function (r) {
				vm.tFinanceProduct = r.tFinanceProduct;
				vm.baseLoan = r.baseLoan;

			})
		},
		reload: function (event) {
			vm.showList = true;

			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				datatype:'json',
				postData:vm.q
            }).trigger("reloadGrid");
		},
		clear:function () {
			vm.loanImageShow=false,
			vm.loanUrl="",
			vm.isNoLook=true
			vm.tFinanceProduct={
				showStatus:0,
				type:"",
				productDetails:'',
				image:""
			}
			vm.reload();
		}
	}
});

